﻿using Hidistro.SaleSystem.Catalog;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class wnxd_SKUSelector : WebControl
    {
        protected override void Render(HtmlTextWriter writer)
        {
            int productId;
            if (int.TryParse(this.Page.Request.QueryString["ProductId"], out productId))
            {
                DataTable skus = ProductBrowser.GetSkus(productId);
                StringBuilder sb = new StringBuilder();
                if (skus != null && skus.Rows.Count > 0)
                {
                    string SkuId = null;
                    IList<string> list = new List<string>();
                    sb.AppendFormat("<input type=\"hidden\" id=\"hiddenProductId\" value=\"{0}\" />", productId);
                    foreach (DataRow row in skus.Rows)
                    {
                        if (!list.Contains((string)row["AttributeName"]))
                        {
                            list.Add((string)row["AttributeName"]);
                            if (SkuId == null && row["ProductId"].ToString() == productId.ToString()) SkuId = row["SkuId"].ToString();
                            sb.Append("<div style=\"padding-top: 1.3em;\" class=\"gg_ys\">");
                            sb.AppendFormat("<p class=\"t\"><span>{0}：</span><p class=\"b\">", row["AttributeName"]);
                            IList<string> list2 = new List<string>();
                            foreach (DataRow row2 in skus.Rows) if ((string.Compare((string)row["AttributeName"], (string)row2["AttributeName"]) == 0) && !list2.Contains((string)row2["ValueStr"])) sb.AppendFormat("<a href=\"javascript:void(0);\" AttributeId=\"{0}\" ValueId=\"{1}\" SkuId=\"{4}\" Pid=\"{5}\" class=\"wnxd_SKUSelector{3}\">{2}</a>", row2["AttributeId"], row2["ValueId"], row2["ValueStr"], SkuId == row2["SkuId"].ToString() ? " cur" : "", row2["SkuId"], row2["ProductId"]);
                            sb.Append("</div>");
                        }
                    }
                    sb.AppendFormat("<input type=\"hidden\" id=\"hiddenSkuId\" value=\"{0}\"  />", SkuId);
                }
                else sb.AppendFormat("<input type=\"hidden\" id=\"hiddenSkuId\" value=\"{0}_0\"  />", productId);
                writer.Write(sb.ToString());
            }
        }
    }
}